/*
 * @copyright fxsen <br/>
 * @date 2022/4/10 0:05 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.platform.web.controllers;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResultJson;
import com.fxsen.commons.sysmanager.core.entity.OperationLog;
import com.fxsen.commons.sysmanager.core.service.IOperationLogService;
import com.fxsen.commons.sysmanager.core.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fxsen
 * @date 2022/4/10 0:05
 * @email <a href="mailto:fxsen@foxmail.com">
 */
@RestController
@RequestMapping("operationLog")
@RequiredPermission("operationLog")
@Api(tags = "系统：日志接口")
@CacheConfig(cacheNames = "dept")
public class OperationLogController  {
    @Autowired
    private IOperationLogService operationLogService;
    @Autowired
    private IUserService userService;
    @ApiOperation(value = "操作日志")
    @GetMapping("operationLogList")
    @RequiredPermission("operationLog:operationLogList")
    public ResultJson operationLog(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                           @RequestParam(value = "limit",required = false,defaultValue = "10") int limit,String name){
        try {
            LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(name),OperationLog::getName,name);
            queryWrapper.eq(OperationLog::getType,1);
            queryWrapper.orderByDesc(OperationLog::getCreateDate);
            Page<OperationLog> operationLogPage = operationLogService.page(new Page<>(page, limit), queryWrapper);
            return ResultJson.success(operationLogPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
    @ApiOperation(value = "登录日志")
    @GetMapping("loginLog")
    @RequiredPermission("operationLog:loginLog")
    @Cacheable(key = "'id:' + #p0 + '_' + #p1 + '_' +  #p3")
    public ResultJson loginLog(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                               @RequestParam(value = "limit",required = false,defaultValue = "10") int limit,
                               String name){
        try {
            LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(name),OperationLog::getName,name);
            queryWrapper.eq(OperationLog::getType,0);
            queryWrapper.orderByDesc(OperationLog::getCreateDate);
            Page<OperationLog> operationLogPage = operationLogService.page(new Page<>(page, limit), queryWrapper);
            return ResultJson.success(operationLogPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
    @ApiOperation(value = "异常日志")
    @GetMapping("exceptionLog")
    @RequiredPermission("operationLog:exceptionLog")
    public ResultJson exceptionLog(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                               @RequestParam(value = "limit",required = false,defaultValue = "10") int limit,
                               String name){
        try {
            LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(name),OperationLog::getName,name);
            queryWrapper.eq(OperationLog::getType,2);
            queryWrapper.orderByDesc(OperationLog::getCreateDate);
            Page<OperationLog> operationLogPage = operationLogService.page(new Page<>(page, limit), queryWrapper);
            return ResultJson.success(operationLogPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.ERROR_ERRO;
        }
    }
}
